package com.testapp.bluetoothtest08.My;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.testapp.bluetoothtest08.Constant.Constants;
import com.testapp.bluetoothtest08.Dialog.DiagnosedDialog;
import com.testapp.bluetoothtest08.Dialog.LogoutDialog;
import com.testapp.bluetoothtest08.Dialog.ModifyPasswordDialog;
import com.testapp.bluetoothtest08.Dialog.RefreshDialog;
import com.testapp.bluetoothtest08.Dialog.YellowRefreshDialog;
import com.testapp.bluetoothtest08.File.ChallengeFile;
import com.testapp.bluetoothtest08.File.Cipher;
import com.testapp.bluetoothtest08.File.CodeFile;
import com.testapp.bluetoothtest08.File.DeviceFile;
import com.testapp.bluetoothtest08.File.GenerateKeyFile;
import com.testapp.bluetoothtest08.File.Params;
import com.testapp.bluetoothtest08.File.ScanCodeFirstRequestFile;
import com.testapp.bluetoothtest08.File.ScanCodeSecondRequestFile;
import com.testapp.bluetoothtest08.File.ScanMachineFile;
import com.testapp.bluetoothtest08.File.Statement;
import com.testapp.bluetoothtest08.File.UserName;
import com.testapp.bluetoothtest08.File.UserScanCodeDataFile;
import com.testapp.bluetoothtest08.Http.OkHttp;
import com.testapp.bluetoothtest08.Judge.IsYellow;
import com.testapp.bluetoothtest08.R;
import com.testapp.bluetoothtest08.cipher.entity.user.UserPrivateDataStr;

public class MyFragment extends Fragment implements View.OnClickListener {
    //视图
    private View v;
    private TextView login_username, my_statement;
    private TextView modify_password, login_out, user_Diagnosed, user_judge, change_key, about_us ,yellow_refresh,zuobi;
    private int server_statement;
    private String local_statement;

    public View getV() {
        return v;
    }

    public void setV(View v) {
        this.v = v;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (v == null) {
            v = inflater.inflate(R.layout.activity_my_fragment, container, false);
        }
        init();
        //获取状态
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttp http = new OkHttp();
                String state = http.httpGet(Constants.GET_INFO_URL);
                if(state!=null){
                    JSONObject s = JSONObject.parseObject(state);
                    System.out.println("码的状态 ----> "+s);
                    String data = s.getString("data");
                    JSONObject d = JSONObject.parseObject(data);
                    int statement = (int) d.get("codeColor");
                    switch(statement){
                        case 0:
                            new Statement().write("正常者");
                            break;
                        case 1:
                            new Statement().write("接触者");
                            break;
                        case 2:
                            new Statement().write("确诊者");
                            break;
                        case 3:
                            new Statement().write("违规者");
                            break;
                    }
                }
            }
        });
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        login_username.setText(new UserName().read());
        my_statement.setText(new Statement().read());

        return v;
    }

    private void init() {
        login_username = v.findViewById(R.id.my_username);
        my_statement = v.findViewById(R.id.my_statement);

        modify_password = v.findViewById(R.id.modify_password);
        login_out = v.findViewById(R.id.login_out);
        user_Diagnosed = v.findViewById(R.id.user_Diagnosed);
        user_judge = v.findViewById(R.id.user_judge);
        change_key = v.findViewById(R.id.change_key);
        about_us = v.findViewById(R.id.about_us);
        yellow_refresh=v.findViewById(R.id.yellow_refresh);

        modify_password.setOnClickListener(this);
        login_out.setOnClickListener(this);
        user_Diagnosed.setOnClickListener(this);
        user_judge.setOnClickListener(this);
        about_us.setOnClickListener(this);
        change_key.setOnClickListener(this);
        yellow_refresh.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.modify_password:
                ModifyPasswordDialog modifyPasswordDialog = new ModifyPasswordDialog(getActivity());
                modifyPasswordDialog.setCancelable(false);
                modifyPasswordDialog.show();
                break;
            case R.id.login_out:
                LogoutDialog logoutDialog = new LogoutDialog(getActivity());
                logoutDialog.setCancelable(false);
                logoutDialog.show();
                break;
            case R.id.user_Diagnosed:
                DiagnosedDialog diagnosedDialog = new DiagnosedDialog(getActivity(), v);
                diagnosedDialog.setCancelable(false);
                diagnosedDialog.show();
                break;
            case R.id.user_judge:
                //蓝牙判断
                Thread yellow = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        new IsYellow().judge();
                    }
                });
                yellow.start();
                try {
                    yellow.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //SN判断
                Thread snYellow = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        new IsYellow().judgeScanMachine();
                    }
                });
                snYellow.start();
                try {
                    snYellow.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //判断
                if(new Statement().read().equals("接触者")){
                    my_statement.setText("接触者");
                }
                Toast.makeText(this.getActivity(), "请求中", Toast.LENGTH_SHORT).show();
                break;
            case R.id.change_key:
                RefreshDialog refreshDialog = new RefreshDialog(getActivity(), v);
                refreshDialog.setCancelable(false);
                refreshDialog.show();
                break;
            case R.id.about_us:
                Params params = new Params();
                DeviceFile deviceFile = new DeviceFile();
                CodeFile codeFile = new CodeFile();
                params.clearDay();
                params.clearKey();
                params.clearTime();
                params.clearLastT();
                deviceFile.clearAll();
                codeFile.clearCode();

//                new ChallengeFile().clear();
//                new GenerateKeyFile().clear();
//                new ScanCodeFirstRequestFile().clear();
//                new ScanCodeSecondRequestFile().clear();
//                new UserScanCodeDataFile().clear();

                new ScanMachineFile().clearAll();

                Toast.makeText(this.getActivity(), "清空！", Toast.LENGTH_SHORT).show();
                break;
            case R.id.yellow_refresh:
                YellowRefreshDialog yellowRefreshDialog = new YellowRefreshDialog(getActivity(), v);
                yellowRefreshDialog.setCancelable(false);
                yellowRefreshDialog.show();
                break;
        }
    }
}
